package clubhouse.motioncommand;

/**
 * @author Anis Darwich & Lionel Croix - Simulation and experimentation on Robot in WSN - WnSN Lab - SJTU
 */
public class NodeControl {

	/**Xbee message start delimiter.*/
	private Integer start_delimiter;
	/**API type of current message.*/
	private Integer api_type;

	/**Source field of current message.*/
	private Integer source;

	/**X coordinate field of current message.*/
	private Integer x_coord;
	/**Y coordinate field of current message.*/
	private Integer y_coord;

	/**Xbee message end delimiter.*/
	private Integer end_delimiter;

	/**
	 * Constructs the NodeControl object directly from the specified node information.
	 * @param nodeId id of the created node.
	 * @param x the X coordinate of the created node.
	 * @param y the Y coordinate of the created node.
	 */
	public NodeControl (Integer nodeId, Integer x, Integer y) {
		this.start_delimiter = 255;
		this.api_type = 1;
		
		this.source = nodeId;
		this.x_coord = x;
		this.y_coord = y;
		
		this.end_delimiter = 254;
	}
	
	/**
	 * Constructs a generic NodeControl object (without id or coordinates). 
	 */
	public NodeControl () {
		this.start_delimiter = 255;
		this.api_type = 1;
		this.end_delimiter = 254;
	}
	
	/**
	 * Return Api type in message.
	 * @return the value of the API type.
	 */
	public Integer getApiType() {
		return this.api_type;
	}
	
	/**
	 * Return source field in message.
	 * @return the value of the source field.
	 */
	public Integer getSource() {
		return this.source;
	}
	
	/**
	 * Return X coordinate field in message.
	 * @return the value of the X coordinate field.
	 */
	public Integer getX() {
		return this.x_coord;
	}
	
	/**
	 * Return Y coordinate field in message.
	 * @return the value of the Y coordinate field.
	 */
	public Integer getY() {
		return this.y_coord;
	}
	
	/**
	 * Returns a string representation of the message written in Hexadecimal format.
	 * @return the string representing the whole message written in Hexadecimal format.
	 */
	public String toHexPrint(){
		return (Integer.toHexString(start_delimiter)+":" + Integer.toHexString(api_type)+":" + Integer.toHexString(source)+":" + Integer.toHexString(x_coord)+":" + Integer.toHexString(y_coord)+":"+ Integer.toHexString(end_delimiter));
	}
	
	/**
	 * Parses a given message from string to NodeControl.
	 * @param packet the message packet received to parse.
	 * @return the NodeControl object created from the received packet.
	 */
	public static NodeControl parse(String packet) {
		NodeControl received = new NodeControl();
		
		received.api_type = Integer.parseInt(packet.substring(2, 4), 16);
		received.source = Integer.parseInt(packet.substring(4, 6), 16);
		received.x_coord = Integer.parseInt(packet.substring(6, 8), 16);
		received.y_coord = Integer. parseInt(packet.substring(8, 10), 16);
		
		//Uncomment the following lines to display received node packet in console.
		/* 
		System.out.print("length " + packet.length()+ "\n"); //
		System.out.print("@ index 0-2: " + packet.substring(0, 2) + "\n"); // ff start delimiter
		System.out.print("@ index 2-4: " + packet.substring(2, 4) + "\n"); // 01 api type
		System.out.print("@ index 4-6: " + packet.substring(4, 6) + "\n"); // 02 source
		System.out.print("@ index 6-8: " + packet.substring(6, 8) + "\n"); // 10 x
		System.out.print("@ index 8-10: " + packet.substring(8, 10) + "\n"); // 20 y
		*/
		
		return received;
	}
}
